#include <stdio.h>
#include <unistd.h>
#include <string.h>
#include "StdThread.h"
#include "StdTcp.h"
#include "../GlobalMessage.h"
#include "DoubleLinkList.h"
#include "StdThreadPool.h"
#define PORT 8080
#define IP "192.168.204.128"

DLlist ClientList;


void ChooseSelect(GLMSG *m,int sock)
{
    switch(m->flag)
    {
    case login:
        InsertDLlistTail(&ClientList,CreateClientInfo(m->fromName,sock));
        printf("登陆了一个新的客户端:账号:%s  套接字:%d\n",m->fromName,sock);
        break;
    case chat:
        DLlist list = FindByElement(&ClientList,m->toName,IsNameEqual);
        if(list.len == 0)
            printf("没找到相应的客户端:%s\n",m->toName);
        else
        {
            //转发
            CInfo *c = (CInfo*)list.head->next->data;
            TcpServerSend(c->sock,m,sizeof(GLMSG));
        }
        ClearDLlist(&list,NULL);   
        break;
    case allchat:
        DLNode* TravelPoint = ClientList.head->next;
        while(TravelPoint != NULL)
        {
            CInfo *c = (CInfo*)TravelPoint->data;
            if(strcmp(c->account,m->fromName) != 0)
            {
                TcpServerSend(c->sock,m,sizeof(GLMSG));
            }
            TravelPoint = TravelPoint->next;
        }
        break;
    case FileStart:
    case sendFile:
        DLlist listF = FindByElement(&ClientList,m->toName,IsNameEqual);
        if(listF.len == 0)
            printf("没找到相应的客户端:%s\n",m->toName);
        else
        {
            //转发
            CInfo *c = (CInfo*)listF.head->next->data;
            TcpServerSend(c->sock,m,sizeof(GLMSG));
        }
        ClearDLlist(&listF,NULL);   
        break;
    default:
        break;
    }
}

void *Thread_Handler(void *arg)
{
    //GLMSG m;
    int clientSock = *(int*)arg;
    while(1)
    {
        GLMSG msg;
        if(TcpServerRecv(clientSock,&msg,sizeof(msg)) == false)
        {
            printf("客户端已下线!\n");
            return NULL;
        };
        //printf("收到客户端的消息:%s\n",msg);

        ChooseSelect(&msg,clientSock);
    }
    close(clientSock);
}

int main()
{
    TcpS *s = InitTcpServer(IP,PORT);
    InitDLlist(&ClientList);
    ThreadP *p = InitThreadPool(10,20,10,60);
    
    //套接字
    // int sock = socket(AF_INET,SOCK_STREAM,0);
    // if(sock < 0)
    // {
    //     perror("socket");
    //     return -1;
    // }

    // struct sockaddr_in addr;
    // addr.sin_family = AF_INET;
    // addr.sin_port = htons(PORT);
    // addr.sin_addr.s_addr = inet_addr(IP);
    // if(bind(sock,(struct sockaddr*)&addr,sizeof(addr)) < 0)
    // {
    //     perror("bind");
    //     return -1;
    // }

    // //最大等待队列  真正握手的函数
    // if(listen(sock,10) < 0)
    // //已经建立连接
    // {
    //     perror("listen");
    //     return -1;
    // }

    // struct sockaddr_in ClientAddr;
    // socklen_t len = sizeof(ClientAddr);
    
    int clientSock = 0;
    //阻塞函数
    while((clientSock = Accept(s)) >= 0)
    {
        // printf("连接进来的客户端socket:%d,地址:%s 端口:%d\n",
        //     clientSock,inet_ntoa(ClientAddr.sin_addr),ntohs(ClientAddr.sin_port));
        //Thread *t = InitThread(Thread_Handler,&clientSock);
        AddPoolTask(p,Thread_Handler,(void*)&clientSock);
    }
    
    ClearThreadPool(p);
    return 0;
}